package com.sphere.freereader.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.sphere.freereader.R;
import com.sphere.freereader.impl.ReaderImageListener;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by aquarius on 2016/8/8.
 */
public class Helper {

    public static Map<String, String> getBookCategories(Context context) {
        String[] cateogory = context.getResources().getStringArray(R.array.book_category);
        String[] urls = context.getResources().getStringArray(R.array.book_category_urls);
        if(urls.length != cateogory.length) {
            throw new IllegalArgumentException("The length of categories is not match the urls length!!");
        }
        Map<String, String> bookCategories = new HashMap<String, String>();
        for (int i = 0; i < cateogory.length; i++) {
            bookCategories.put(cateogory[i], urls[i]);
        }
        return bookCategories;
    }


    public static void loadImageForNetwork(ImageView imageView, ImageLoader imageLoader, String iconPath,
                                           int defaultIconRes) {
        try {
            imageView.setImageResource(defaultIconRes);
            ImageLoader.ImageListener listener = ReaderImageListener.getInstance(imageView, defaultIconRes, R.drawable.img_no_book_cover);
            imageView.setTag(iconPath);
            if (!TextUtils.isEmpty(iconPath)) {
                imageLoader.get(iconPath, listener);
            }
        } catch (Exception e) {
            throw new RuntimeException("load image error," + e.toString());
        }

    }

    /**
     * 获取控件的大小
     * @param view 要获取尺寸的控件
     * @return 控件的尺寸,[0]表示宽度, [1]表示高度
     */
    public static int[] getViewSize(final View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        int[] size = new int[2];
        size[0] = view.getMeasuredWidth();
        size[1] = view.getMeasuredHeight();
        return size;
    }

    /**
     * 获得状态栏的高度
     * @param context
     * @return 状态栏的高度
     */
    public static int getStatusBarHeight(Context context){
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0;
        int statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }

    /**
     * 获得ActionBar的高度
     * @param context
     * @return ActionBar的高度
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static int getActionBarHeight(Context context) {
        TypedValue tv = new TypedValue();
        int resId = 0;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            resId = android.R.attr.actionBarSize;
        } else {
            resId = android.support.v7.appcompat.R.attr.actionBarSize;
        }
        if (context.getTheme().resolveAttribute(resId, tv, true)) {
            return TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
        }
        return 0;
    }

    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int dp2sp(Context context,float dipValue){
        return px2sp(context, dp2px(context, dipValue));
    }

    public static void toastLong(Context context, CharSequence text) {
        Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    }

    public static void toastShort(Context context, CharSequence text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }

}
